//
//  BankCardViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/8/11.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "BankCardViewController.h"

@interface BankCardViewController ()

@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;

@end

@implementation BankCardViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = NO;
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

    self.navigationItem.title = @"选择支付方式";
}

- (void)loadData {
    self.moneyLabel.text = [NSString stringWithFormat:@"¥%@",self.payAccount];
}

- (IBAction)alipayBtnAction:(id)sender {
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    SQUserModel *model = [SQUserModel yy_modelWithDictionary:dic];
    NSString *dateStr = [NSString getNowTimeTimestamp];
    
    NSDictionary *param = @{@"name" : model.userName, @"priceall" : _payAccount,  @"date" : dateStr};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    [manager GET:[Host stringByAppendingString:@"zhifubao2?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *orderString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"lianfushangquan" callback:^(NSDictionary *resultDic) {
                MBProgressHUD *resultHUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
                resultHUD.mode = MBProgressHUDModeText;
                switch ([[resultDic objectForKey:@"resultStatus"] integerValue]) {
                    case 9000:{
                        resultHUD.labelText = @"付款成功!";
                        sleep(2);
//                        [self showEvaluateAlertView];
                    }
                        break;
                    case 6001:{
                        sleep(1);
                        resultHUD.labelText = @"取消付款!";
                    }
                        break;
                    default:
                        break;
                }
                [resultHUD hide:YES afterDelay:1.5];
                [self.view addSubview:resultHUD];
                
            }];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}

//- (IBAction)wechatBtnAction:(id)sender {
//    
//    //判断是否安装微信
//    if([WXApi isWXAppInstalled]) {
//        // 监听一个通知
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"ORDER_PAY_NOTIFICATION" object:nil];
//        
//        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//        
//        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//        
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
//        
//        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//        manager.securityPolicy.allowInvalidCertificates = YES;
//        
//        [manager GET:@"http://www.beidikeji.com/WxpayAPI_php_v3/example/jsapi.php" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            //配置调起微信支付所需要的参数
//            
//            NSLog(@"%@", responseObject);
//            //        PayReq *req  = [[PayReq alloc] init];
//            //
//            //        req.partnerId = [responseObject objectForKey:@"partnerid"];
//            //        req.prepayId = [responseObject objectForKey:@"prepayid"];
//            //        req.package = [responseObject objectForKey:@"package"];
//            //        req.nonceStr = [responseObject objectForKey:@"noncestr"];
//            //        req.timeStamp = [[responseObject objectForKey:@"timestamp"]intValue];
//            //        req.sign = [responseObject objectForKey:@"sign"];
//            
//            //调起微信支付
//            //        if ([WXApi sendReq:req]) {
//            //            NSLog(@"吊起成功");
//            //        }
//            
//            [WXApi openWXApp];
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"%@",error);
//        }];
//    }
//}

#pragma mark - 收到支付成功的消息后作相应的处理
- (void)getOrderPayResult:(NSNotification *)notification
{
    if ([notification.object isEqualToString:@"success"]) {
        NSLog(@"支付成功");
    } else {
        NSLog(@"支付失败");
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
